import 'dart:convert';

import 'package:hxdj/common/base_page/base_page.dart';
import 'package:hxdj/common/dio/dio_http.dart';
import 'package:hxdj/common/my_constant.dart';

class RecommendTechnicianViewModel extends BaseViewModel {
  // 请求的数据.
  // 这里只是模拟数据, 实际开发中, 需要根据接口返回的数据来更新.
  // 假设接口返回的数据是一个列表, 列表中包含多个 TechnicianModel 对象.
  // 这里只是简单地模拟一个 TechnicianModel 对象, 实际开发中, 需要根据接口返回的数据来更新.
  List<TechnicianModel> dataList = [];

  int page = 1;
  int limit = MyConstant.pageSize;

  bool isHave = true;

  @override
  requestData(
      {Function(dynamic)? onSuccess,
      Function(dynamic)? onError,
      bool isFresh = false,
      bool isCleanDate = false}) async {
    if (isFresh) {
      setState(ViewState.loading);
    }

    if (isCleanDate) {
      dataList.clear();
      page = 1;
      isHave = true;
    }

    // 去请求服务器数据.
    Map<String, dynamic> resultMap = await DioHttpManager()
        .request("get_recomment_technician_list", method: DioHttpManager.post, data: {"page": page, "limit": limit});

    page += 1;

    if (resultMap['code'] == 0) {
      List<dynamic> resultList = resultMap["data"] ?? [];
      if (resultList.length < limit) {
        //小于分页数据, 就没有更多数据了
        isHave = false;
      } else {
        isHave = true;
      }

      for (var element in resultList) {
        dataList.add(TechnicianModel.fromMap(element));
      }
      // 更新页面.
      if (dataList.isEmpty) {
        setState(ViewState.empty);
      } else {
        setState(ViewState.normal);
      }
     
      
     
    } else {
      // 更新页面.
      setState(ViewState.error);
    }
  }
}

class TechnicianModel {
  final double? lon;
  final double? lat;
  final int? tId;
  final int? mId;
  final String? name;
  final String? logo;
  final List<String>? imgs;
  final int? sex;
  final String? desc;
  final int? orderQuantity;
  final int? isFalse;
  final double? distance;
  final String? distanceStr;
  final int? dateType;

  TechnicianModel({
    this.lon,
    this.lat,
    this.tId,
    this.mId,
    this.name,
    this.logo,
    this.imgs,
    this.sex,
    this.desc,
    this.orderQuantity,
    this.isFalse,
    this.distance,
    this.distanceStr,
    this.dateType,
  });

  factory TechnicianModel.fromJson(String str) => TechnicianModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory TechnicianModel.fromMap(Map<String, dynamic> json) => TechnicianModel(
        lon: json["lon"]?.toDouble(),
        lat: json["lat"]?.toDouble(),
        tId: json["t_id"],
        mId: json["m_id"],
        name: json["name"],
        logo: json["logo"],
        imgs: json["imgs"] == null ? [] : List<String>.from(json["imgs"]!.map((x) => x)),
        sex: json["sex"],
        desc: json["desc"],
        orderQuantity: json["order_quantity"],
        isFalse: json["is_false"],
        distance: json["distance"]?.toDouble(),
        distanceStr: json["distance_str"],
        dateType: json["date_type"],
      );

  Map<String, dynamic> toMap() => {
        "lon": lon,
        "lat": lat,
        "t_id": tId,
        "m_id": mId,
        "name": name,
        "logo": logo,
        "imgs": imgs == null ? [] : List<dynamic>.from(imgs!.map((x) => x)),
        "sex": sex,
        "desc": desc,
        "order_quantity": orderQuantity,
        "is_false": isFalse,
        "distance": distance,
        "distance_str": distanceStr,
        "date_type": dateType,
      };
}
